<template>
  <div class="app-container">
    <el-container>
      <!-- <el-main>
        <el-table class="user-table" key="tableKey" :data="list" v-loading="listLoading" element-loading-text="加载中" border fit stripe highlight-current-row style="width: 100%">
          <el-table-column align="center" label="系统编号" width="80">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
</el-table-column>

<el-table-column align="center" label="类目">
  <template slot-scope="scope">
              <span>{{ scope.row.type }}</span>
            </template>
</el-table-column>

<el-table-column align="center" label="修改时间" width="160">
  <template slot-scope="scope">
              <span>{{ scope.row.updated_at }}</span>
            </template>
</el-table-column>

<el-table-column align="center" label="操作">
  <template slot-scope="scope">
              <el-button plain size="small" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
            </template>
</el-table-column>
</el-table>

<div class="pagination-container text-right user-page">
  <el-pagination background :total="pagination.total" :current-page="listQuery.page" :page-size="listQuery.limit"
    layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
    @current-change="handleCurrentChange" />
</div>
</el-main> -->
      <el-form
        class="user-form"
        :model="form"
        label-position="center"
        label-width="80px"
      >
        <el-form-item label="头像" prop="type" required>
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="content">
          <el-input
            id="type"
            v-model="form.type"
            size="small"
            maxlength="32"
            style="width:200px"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="content">
          <el-input
            id="type"
            v-model="form.type"
            size="small"
            maxlength="32"
            style="width:200px"
          />
        </el-form-item>
        <el-form-item>
          <!--<el-input size="small" type="hidden" disabled v-model="temp.id" maxlength="32" style="width:200px"></el-input>-->
          <el-button
            size="small"
            type="primary"
            style="text-align:right;"
            @click="handleSave"
            >确 定</el-button
          >
          <el-button
            size="small"
            type="primary"
            style="text-align:right;"
            @click="changePassword"
            >修改密码</el-button
          >
        </el-form-item>
      </el-form>
    </el-container>
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form
        class="small-space"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="旧密码">
          <el-input v-model="temp.oldPassword" />
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="temp.newPassword" />
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="temp.confirmPassword" />
        </el-form-item>
        <!-- <el-form-item label="密码">
          <el-input v-model="temp.password" v-if="dialogStatus == 'create'"></el-input>
          <el-input v-model="temp.password" v-else placeholder="不填写密码则不修改密码"></el-input>
        </el-form-item>
        <el-alert title="* 普通管理员无《系统管理》模块的权限" type="info"> -->
        <!-- </el-alert> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          v-if="dialogStatus == 'create'"
          type="primary"
          @click="create"
          >新 增</el-button
        >
        <el-button v-else type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Save, List, UserSave, UserDel } from "@/api/aboutus";
import { getInfo } from "@/api/user";
import { getToken } from "@/utils/auth";
import { uploadPath } from "@/utils/common";
import "@wangeditor/editor/dist/css/style.css";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

export default {
  components: { Editor, Toolbar },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      editor: null,
      toolbarConfig: {},
      editorConfig: {
        placeholder: "请输入内容...",
        MENU_CONF: {}
      },
      mode: "default",
      uploadPath: uploadPath,
      list: null,
      pagination: {},
      listLoading: false,
      dialogFormVisible: false,
      dialogTitle: "修改密码",
      dialogStatus: null,
      currentCompanyId: 0,
      currentCompanyJoin: 0,
      form: {},
      temp: {
        content: "",
        type: "",
        id: 0
      },

      listQuery: {
        page: 1,
        limit: 10,
        type: "",
        content: ""
      },
      header: {
        Authorization: getToken()
      },
      imageUrl: ""
    };
  },
  created() {
    // this.$forceUpdate();
    this.getData();

    this.editorConfig.MENU_CONF["uploadImage"] = {
      server: uploadPath,
      fieldName: "file",
      // 单个文件的最大体积限制，默认为 2M
      maxFileSize: 2 * 1024 * 1024, // 2M
      // 最多可上传几个文件，默认为 100
      maxNumberOfFiles: 10,
      // 选择文件时的类型限制，默认为 ['image/*'] 。如不想限制，则设置为 []
      allowedFileTypes: ["image/*"],
      // 自定义增加 http  header
      headers: {
        Authorization: getToken()
      },
      // 跨域是否传递 cookie ，默认为 false
      withCredentials: false,
      // 超时时间，默认为 10 秒
      timeout: 5 * 1000, // 5 秒
      // 自定义插入图片
      customInsert(res, insertFn) {
        // res 即服务端的返回结果
        // 从 res 中找到 url alt href ，然后插图图片
        console.log(res.data.filepath);
        insertFn(res.data.filepath, "", "");
      }
    };
    this.toolbarConfig = {
      excludeKeys: ["insertVideo", "uploadVideo", "group-video"]
    };
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
    },
    getData() {
      this.listLoading = true;
      getInfo().then(response => {
        this.form = response.data.data;
      });
      this.listLoading = false;
    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },

    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },

    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },

    handleStatus(row, status) {
      if (status == 1) {
        var str = "启用";
      } else {
        var str = "停用";
      }
      this.$confirm("去定要" + str + "该房间吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          Status({ id: row.id }).then(response => {
            if (response.code === 0) {
              this.$message({
                message: response.msg,
                type: "success"
              });
              this.dialogFormVisible = false;
              this.getList();
            } else {
              this.$message({
                message: response.msg,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "操作已取消"
          });
        });
    },

    showCreate() {
      this.user = {
        username: "",
        phone: "",
        company_id: this.listQuery.company_id
      };
      this.dialogStatus = "create";
      (this.dialogTitle = "添加"), (this.dialogFormVisible = true);
    },

    handleEdit(id) {
      Info({ id: id }).then(res => {
        if (res.code === 0) {
          this.temp = res.data;
        } else {
          this.$message.error(res.msg);
        }
      });
    },

    // handleSave() {
    //   if (this.temp.id < 1) {
    //     this.$message.error("请先点击编辑按钮，选择你要修改的内容！")
    //     return;
    //   }
    //   Save(this.temp).then((response) => {
    //     if (response.code === 0) {
    //       this.$message({
    //         message: response.msg,
    //         type: 'success',
    //       })
    //       this.dialogFormVisible = false
    //       this.getList()
    //     } else {
    //       this.$message({
    //         message: response.msg,
    //         type: 'error',
    //       })
    //     }
    //   })
    // },

    handleSaveUser() {
      UserSave(this.user).then(response => {
        if (response.code === 0) {
          this.$message({
            message: response.msg,
            type: "success"
          });
          this.dialogFormVisible = false;
          this.getList();
        } else {
          this.$message({
            message: response.msg,
            type: "error"
          });
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    changePassword() {
      // 修改密码
      this.dialogFormVisible = true;
    }
  }
};
</script>

<style lang="scss" scoped>
/* .table-container {
  margin: 20px 0;
} */
.company {
  border: 1px solid #4f72e4;
}

.user-form {
  margin: 30px 50px 0px 0px;
}

::v-deep.el-form {
  .el-form-item__label {
    color: #fff !important;
  }
}

.user-table {
  margin-top: 20px;
}

.user-page {
  margin-top: 20px;
}

.ql-editor {
  height: 400px;
}

.avatar-uploader ::v-deep.el-upload {
  border: 1px dashed #ffffff;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
